﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace Students
{
    public class DAStudents
    {
        AccessBase bases;
        public bool Insert(ref BOStudents student)
        {
            try
            {
                bases = new AccessBase();
                bases.cnn.Open();
                string sql = "INSERT into Students([Firstname],[LastName],[Age],[IsFullTime],[Username],[Password])Values(@Firstname,@Lastname,@Age,@IsFullTime,@Username,@Password)";
                OleDbCommand cmd = new OleDbCommand(sql, bases.cnn);
                cmd.Parameters.AddWithValue("@Firstname", student.Firstname);
                cmd.Parameters.AddWithValue("@Lastname", student.Lastname);
                cmd.Parameters.AddWithValue("@Age", student.Age);
                cmd.Parameters.AddWithValue("@IsFullTime", student.IsFullTime);
                cmd.Parameters.AddWithValue("@Username", student.Username);
                cmd.Parameters.AddWithValue("@Password", student.Password);
                if (cmd.ExecuteNonQuery() != 1)
                    return false;
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                bases.cnn.Close();
            }
        }        

        public BOStudents GetStudent(string userName, string passWord)
        {
            try
            {
                bases = new AccessBase();
                bases.cnn.Open();
                OleDbCommand command = new OleDbCommand("SELECT * from  Students Where Username='" + userName + "' and Password='" + passWord + "'", bases.cnn);
                using (var reader = command.ExecuteReader())
                {
                    reader.Read();
                    var std = new BOStudents
                    {
                        Id = int.Parse(reader[0].ToString()),
                        Firstname = reader[1].ToString(),
                        Lastname = reader[2].ToString(),
                        Age = int.Parse(reader[3].ToString()),
                        IsFullTime = bool.Parse(reader[4].ToString()),
                        Username = reader[5].ToString(),
                        Password = reader[6].ToString()
                    };
                    bases.cnn.Close();
                    return std;
                }

           }
             catch
            {

                return null;
            }
        }
    }
}
